﻿namespace MockingBird.Simulator.Framework.Handlers
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Threading;
    using System.Xml;
    using Common.Infrastructure.Logging;
    using Configuration;
    using Interfaces;
    using MockingBird.Simulator.Framework.Persistence;

    public sealed class BasicSoapMessageHandler : MessageHandlerBase
    {

        #region "implementation of base properties"

        /// <summary>
        /// Read-Only property that returns the current Soap Request Message
        /// </summary>
        public override XmlDocument Request { get; protected set; }

        /// <summary>
        /// Read-Only Property that returns the current Soap Response Message
        /// </summary>
        public override XmlDocument Response { get; protected set; }

        /// <summary>
        /// read-only property that returns the short file name of the current soap Response message
        /// </summary>
        public override string ResponseFileName { get; protected set; }

        #endregion

        #region "constructor(s)"


        /// <summary>
        /// Default constructor
        /// </summary>
        public BasicSoapMessageHandler() : base() { }

        public BasicSoapMessageHandler(ILogger logger, IHandlerConfigurationManager configManager, IResponseBuilder responseBuilder, ISoapFaultBuilder faultBuilder, IRequestMessageLogger requestMessageLogger, IResponseMessageLogger responseMessageLogger, ApplicationConfiguration appConfig)
            : base(logger, configManager, responseBuilder, faultBuilder, requestMessageLogger, responseMessageLogger, appConfig)
        {
            Debug.WriteLine(string.Format("BasicSoapMessageHandler.ctor on thread ID: {0}", Thread.CurrentThread.ManagedThreadId));

            this.Request = new XmlDocument();
            this.Response = new XmlDocument();
        }

        #endregion

        #region IMessageHandler Members

        public override void HandleMessage(IExecutionContext context)
        {
            this.logger.Log("BasicSoapMessageHandler:HandleMessage()");
            base.HandleMessage(context);
        }

        #endregion



        protected override void ExecuteFileBasedResponse(ResponseType response)
        {
            this.logger.Log("BasicSoapMessageHandler:ExecuteFileBasedResponse()");

            //Find the response Root for the response file
            this.responseRoot = this.appConfiguration.GetResponseRootForEndpoint(context.EndpointName);

            //Update the responseFileName to be a complete path
            this.ResponseFileName = Path.Combine(responseRoot, response.FileName);

            try
            {
                Response = responseBuilder.BuildResponse(this.ResponseFileName, null);
            }
            catch (Exception e)
            {
                Response = faultBuilder.BuildSoapFault(e);
            }
        }



    }
}
